using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace BookBarter
{
    public class TempTradeRoute:ICloneable
    {
        private static List<TradeRoute> tradeRoutes = new List<TradeRoute>();

        //class variables
        private TempTradeLink firstLink;
        private TempTradeLink lastLink;
        private int size;


        public TempTradeLink FirstLink
        {
            get { return firstLink; }
            set { firstLink = value; }
        }
        public TempTradeLink LastLink
        {
            get { return lastLink; }
            set { lastLink = value; }
        }
        public int Size
        {
            get { return size; }
            private set { size = value; }
        }

        public TempTradeRoute()
        {
            this.Size = 0;
            
        }


        /// <summary>
        /// Adds A new TradeLink to the TradeRoute
        /// </summary>
        /// <param name="newLink">Link to be added</param>
        public void AddTradeChainLink(TempTradeLink newLink)
        {
            if (this.FirstLink == null)
            {
                this.FirstLink = newLink;
                this.LastLink = newLink;
            }
            else
            {
                this.LastLink.NextTradeLink = newLink;
                this.LastLink = newLink;
                this.LastLink.NextTradeLink = null;
            }
        }

        /// <summary>
        /// Makes a TradeRoute Object with the properties of the temp object
        /// </summary>
        /// <returns>Returns new TradeRoute Object</returns>
        public TradeRoute MakePermanent()
        {
            TradeRoute newRoute = TradeRoute.CreateNewTradeRoute();
            TempTradeLink currentLink = this.FirstLink;

            while (currentLink != null)
            {
                newRoute.AddTradeLink(currentLink.Have, currentLink.Want);
                currentLink = currentLink.NextTradeLink;
            }
            newRoute.Save();
            return newRoute;
        }

        public bool IncludesPerson(Person user)
        {
            TempTradeLink link = this.FirstLink;
            while (link != null)
                if (link.Have.Person == user)
                    return true;
                else
                    link = link.NextTradeLink;
            return false;
        }

        /// <summary>
        /// Cancels a trade if it has been rejected
        /// </summary>
        public void CancelTrade()
        {
            //needs to be completed
        }

        
        /// <summary>
        /// Clones the current object and returns the cloned object
        /// </summary>
        /// <returns>Returns the cloned object</returns>
        public object Clone()
        {
            return this.MemberwiseClone();
        }

       
      
    }
}
